package com.wxd.framework.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wxd.framework.common.Criteria;
import com.wxd.framework.common.FrameConstants;
import com.wxd.framework.common.page.Pagination;
import com.wxd.framework.common.web.RequestUtils;
import com.wxd.framework.entity.Role;
import com.wxd.framework.entity.User;
import com.wxd.framework.service.RoleService;
import com.wxd.framework.vo.TreeItem;

@Controller
@RequestMapping("/")
public class RoleController extends BaseController {

	@Autowired
	private RoleService roleService;
	
	/**
	 * TO:角色管理页面
	 * @return
	 */
	@RequestMapping("/toRoleManager.do")
	public String toRoleManager(ModelMap model){
		model.addAttribute(FrameConstants.ADMIN_ROLE_KEY, FrameConstants.ADMIN_ROLE);
		return "frame/systemManager/roleManager";
	}
	
	/**
	 * 停止/恢复角色
	 * @param request
	 * @param response
	 */
	@RequestMapping("/stopOneRole.do")
	public void stopOneRole(HttpServletRequest request, HttpServletResponse response){
		String currUser = super.getCurrUser(request).getUserName();
		String id = RequestUtils.getQueryParam(request, "id");
		String isStopedStr = RequestUtils.getQueryParam(request, "isStoped");
		boolean isStoped = Boolean.parseBoolean(isStopedStr);
		roleService.stopOneRole(id, isStoped, currUser);
		super.renderJsonExcuteSuccess(response);
	}
	
	/**
	 * 查找角色下的所有用户
	 * @param request
	 * @param response
	 */
	@RequestMapping("/getUsersByRoleId.do")
	public void getUsersByRoleId(HttpServletRequest request, HttpServletResponse response){
		String roleId = RequestUtils.getQueryParam(request, "roleId");		
		List<User> list = roleService.getUsersByRoleId(roleId);
		super.renderList(list, response);
	}
	
	/**
	 * 根据角色获取菜单列表
	 * @param request
	 * @param response
	 */
	@RequestMapping("/getMenuTreeByRoleId.do")
	public void getMenuTreeByRoleId(HttpServletRequest request, HttpServletResponse response){
		String roleId = RequestUtils.getQueryParam(request, "roleId");
		List<TreeItem> list = roleService.getMenuTreeByRoleId(roleId);
		super.renderList(list, response);
	}

	/**
	 * 保存角色的菜单权限
	 * @param request
	 * @param response
	 */
	@RequestMapping("/saveRoleMenuForRole.do")
	public void saveRoleMenuForRole(HttpServletRequest request, HttpServletResponse response){
		String roleId = RequestUtils.getQueryParam(request, "roleId");
		String menuIds = RequestUtils.getQueryParam(request, "menuIds");
		try{
			roleService.saveRoleMenuForRole(roleId, menuIds);
			super.renderJsonExcuteSuccess(response);
		}catch(Exception ex){
			super.renderException(ex, response);
		}
	}
	
	/**
	 * 新增角色
	 * @param request
	 * @param response
	 */
	@RequestMapping("/addOneRole.do")
	public void addOneRole(HttpServletRequest request, HttpServletResponse response){		
		String currUser = super.getCurrUser(request).getUserName();
		String roleName = RequestUtils.getQueryParam(request, "roleName");
		String roleDesc = RequestUtils.getQueryParam(request, "roleDesc");
		Role role = new Role(roleName, roleDesc, currUser);
		try{
			role = roleService.addOneRole(role);
			if (StringUtils.isNotBlank(role.getMessage())) {
				super.renderJsonExcuteFail(role.getMessage(), response);
			}else {
				super.renderJsonExcuteSuccess(response);			
			}
		}catch(Exception ex){
			super.renderException(ex, response);
		}		
	}
	
	/**
	 * 删除角色
	 * @param request
	 * @param response
	 */
	@RequestMapping("/deleteOneRole.do")
	public void deleteOneRole(HttpServletRequest request, HttpServletResponse response){
		String id = RequestUtils.getQueryParam(request, "id");
		roleService.deleteOneRole(id);
		super.renderJsonExcuteSuccess(response);
	}
	
	/**
	 * 编辑角色
	 * @param request
	 * @param response
	 */
	@RequestMapping("/editOneRole.do")
	public void editOneRole(HttpServletRequest request, HttpServletResponse response){
		String currUser = super.getCurrUser(request).getUserName();
		String id = RequestUtils.getQueryParam(request, "id");
		String roleDesc = RequestUtils.getQueryParam(request, "roleDesc");
		Role role = new Role();
		role.setId(id);
		role.setRoleDesc(roleDesc);
		role.setUpdater(currUser);
		role.setUpdateTime(new Date());
		roleService.editOneRole(role);
		super.renderJsonExcuteSuccess(response);
	}
	
	/**
	 * 根据Id获取角色对象
	 * @param request
	 * @param response
	 */
	@RequestMapping("/getOneRoleById.do")
	public void getOneRoleById(HttpServletRequest request, HttpServletResponse response){
		String id = RequestUtils.getQueryParam(request, "id");
		Role role = roleService.getOneRoleById(id);
		super.renderOneObject(role, response);		
	}
	
	/**
	 * 获取角色信息（分页查询）
	 * @param response
	 */
	@RequestMapping("/getRolesInPage.do")
	public void getRolesInPage(HttpServletRequest request, HttpServletResponse response){
		Criteria criteria = super.getPaginationCriteria(request);
		Pagination dataPage = roleService.getRolesInPage(criteria);
		super.renderDatePagination(dataPage, response);
	}
}
